﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CRM.DataAccess.Entities;
using CRM.Business.Services;
using CRM.Encryption;
using Telerik.Web.UI;

namespace Web.Account
{
    public partial class Roles : System.Web.UI.Page
    {

        static List<ROLE> lRole;
        static USER user;
        static ROLE r;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                return;
            USER user = (USER)Session["User"];
            if (user != null)
            {
                r = RoleService.GetRoleByID((long)user.ROLE_ID);
                if (((bool)user.IS_ADMIN) || ((bool)r.IS_MANAGER))
                {
                    lRole = bindGrid("", null, null);
                    RadGrid1.DataSource = lRole;
                    RadGrid1.DataBind();
                }
                else
                {
                    Response.Redirect("../Home.aspx");
                }
            }
            else
            {
                RadAjaxManager.GetCurrent(this).ResponseScripts.Add(@"ShowMessageAndRedirect('" + "Please login before continue !" + "','" + "Login.aspx" + "');");
            }
        }

        private List<ROLE> bindGrid(string RoleName, bool? isActive, bool? isManager)
        {
            List<ROLE> lRole = RoleService.GetRoleByCondition(RoleName, isActive, isManager);
            if (lRole == null)
            {
                lRole = new List<ROLE>();
            }
            return lRole;

        }
        protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
        {
            GridDataItem dataItem = e.Item as GridDataItem;
            switch (e.CommandName)
            {
                case "Edit":
                    Response.Redirect("EditRole.aspx?id=" + dataItem["ID"].Text);
                    break;
                case "Delete":
                    long roleID = Convert.ToInt64(dataItem["ID"].Text);
                    int deleterole = RoleService.DeleteRole(roleID);
                    if (deleterole == 1)
                    {
                        lRole.Remove(lRole.Where(x => x.ID == roleID).FirstOrDefault<ROLE>());
                        RadAjaxManager.GetCurrent(this).ResponseScripts.Add(@"ShowMessage('" + "Delete role successful !" + "');");
                        //ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "alert('Delete role successful !');", true);
                    }
                    else
                        RadAjaxManager.GetCurrent(this).ResponseScripts.Add(@"ShowMessage('" + "Can not delete this role !" + "');");
                        //ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "alert('Cannot delete this role !');", true);
                    break;
                default:
                    break;
            }
        }
        protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            RadGrid1.DataSource = lRole;
        }

        protected void btnSearch_ServerClick(object sender, EventArgs e)
        {
            bool? isActive = false;
            bool? isManager = false;
            if (RadButton1.Checked == true)
                isActive = true;
            if (RadButton2.Checked == true)
                isActive = false;
            if (RadButton3.Checked == true)
                isActive = null;
            if (RadButton4.Checked == true)
                isManager = true;
            if (RadButton5.Checked == true)
                isManager = false;
            if (RadButton6.Checked == true)
                isManager = null;

            lRole = bindGrid(txtRoleName.Text.Trim(), isActive, isManager);
            RadGrid1.DataSource = lRole;
            RadGrid1.DataBind();
        }
        protected void btnAddRole_ServerClick(object sender, EventArgs e)
        {
            Response.Redirect("AddNewRole.aspx");
        }
        protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridPagerItem)
            {
                GridPagerItem oGridPagerItem = (GridPagerItem)e.Item;
                RadComboBox oRadComboBox = (RadComboBox)oGridPagerItem.FindControl("PageSizeComboBox");
                oRadComboBox.Width = Unit.Pixel(50);
            }
        }
        protected void chbActive_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chk = (CheckBox)sender;
            bool status = chk.Checked;
            GridDataItem item = (GridDataItem)chk.NamingContainer;
            long roleID = (long)item.GetDataKeyValue("ID");

            ROLE updateRoleActive = new ROLE();
            updateRoleActive = RoleService.GetRoleByID(roleID);
            updateRoleActive.ACTIVE = status;
            //update thong qua USER_DETAIL
            int updateRole = RoleService.UpdateRole(updateRoleActive);
            if (updateRole != 1)
            {
                RadAjaxManager.GetCurrent(this).ResponseScripts.Add(@"ShowMessage('" + "Can not update status for this Role !" + "');");
                //ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "alert('Can not update status for this Role !');", true);
            }
        }
        protected void chbIsManager_CheckedChanged(object sender, EventArgs e)
        {
            bool error = false;
            CheckBox chk = (CheckBox)sender;
            bool status = chk.Checked;
            GridDataItem item = (GridDataItem)chk.NamingContainer;
            long roleID = (long)item.GetDataKeyValue("ID");
            ROLE updateRoleManager = new ROLE();
            updateRoleManager = RoleService.GetRoleByID(roleID);
            updateRoleManager.IS_MANAGER = status;
            int updateRole = RoleService.UpdateRole(updateRoleManager);
            if (updateRole != 1)
            {
                error = true;
            }
            if (error)
                RadAjaxManager.GetCurrent(this).ResponseScripts.Add(@"ShowMessage('" + "Can not update is manager status for this role !" + "');");
                //ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "alert('Can not update is_manager status for this role !');", true);
        }
        protected void lbtnExportToCSV_ServerClick(object sender, EventArgs e)
        {
            RadGrid1.ExportSettings.IgnorePaging = true;
            RadGrid1.ExportSettings.OpenInNewWindow = true;
            RadGrid1.ExportSettings.UseItemStyles = true;
            DateTime dt = DateTime.Now;
            RadGrid1.ExportSettings.FileName = "Roles_" + dt.Year + dt.Month + dt.Day;
            RadGrid1.MasterTableView.GetColumn("TOOLS").Display = false;
            RadGrid1.MasterTableView.ExportToCSV();
            RadGrid1.Rebind();
        }
    }
}